/****** Object:  Table [dbo].[AppSchedulerLocks]    Script Date: 11/09/2006 11:21:43 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[AppSchedulerLocks](
	[Id] [bigint] IDENTITY(1,1) NOT NULL,
	[LockedTime] [datetime] NOT NULL,
	[TaskName] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
	[MachineName] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 CONSTRAINT [PK_AppSchedulerLocks] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

/****** Object:  StoredProcedure [dbo].[SchedulerAcquireTaskLock]    Script Date: 11/09/2006 11:22:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[SchedulerAcquireTaskLock] 
(
	@taskName nvarchar(50),
	@machineName nvarchar(50)
)
AS
SET NOCOUNT ON

UPDATE AppSchedulerLocks
	SET MachineName = @machineName, LockedTime = GETDATE()
	WHERE TaskName = @taskName AND MachineName = ''

RETURN @@ROWCOUNT

/****** Object:  StoredProcedure [dbo].[SchedulerAddTaskLock]    Script Date: 11/09/2006 11:22:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[SchedulerAddTaskLock] 
(
	@taskName nvarchar(50)
)
AS
SET NOCOUNT ON

DECLARE @Id bigint
SET @Id = 0

SELECT @Id = Id FROM AppSchedulerLocks WHERE TaskName = @taskName
IF(@Id < 1)
BEGIN

INSERT INTO AppSchedulerLocks (LockedTime, MachineName, TaskName)
	VALUES (GETDATE(), '', @taskName)

END

RETURN SCOPE_IDENTITY()

/****** Object:  StoredProcedure [dbo].[SchedulerDeleteTaskLock]    Script Date: 11/09/2006 11:22:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[SchedulerDeleteTaskLock] 
(
	@taskName nvarchar(50),
	@machineName nvarchar(50)
)
AS
SET NOCOUNT ON

DELETE AppSchedulerLocks
	WHERE TaskName = @taskName AND MachineName = @machineName

RETURN

/****** Object:  StoredProcedure [dbo].[SchedulerReleaseAllLocks]    Script Date: 11/09/2006 11:22:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[SchedulerReleaseAllLocks] 
(
	@machineName nvarchar(50)
)
AS
SET NOCOUNT ON

UPDATE AppSchedulerLocks
	SET MachineName = '', LockedTime = GETDATE()
	WHERE MachineName = @machineName

RETURN

/****** Object:  StoredProcedure [dbo].[SchedulerReleaseTaskLock]    Script Date: 11/09/2006 11:23:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[SchedulerReleaseTaskLock] 
(
	@taskName nvarchar(50),
	@machineName nvarchar(50)
)
AS
SET NOCOUNT ON

UPDATE AppSchedulerLocks
	SET MachineName = '', LockedTime = GETDATE()
	WHERE TaskName = @taskName AND MachineName = @machineName

RETURN
